package com.controller;


import com.bean.DeliveryAddress;
import com.common.R;
import com.service.DeliveryAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
@ResponseBody
@CrossOrigin
//注意，该部分需要和前端对应，但是根据自己的代码视情况而定，一定要一致。
@RequestMapping("/DeliveryAddressController")
public class DeliveryAddressController {
    @Autowired
    private DeliveryAddressService deliveryAddressService;


    @GetMapping("/getDeliveryAddressById")
    private R getDeliveryAddressById(String daId){
        try {
            DeliveryAddress deliveryAddressById = deliveryAddressService.getDeliveryAddressById(daId);
            return R.ok().data(deliveryAddressById);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }
    @PostMapping("/saveDeliveryAddress")
//    在这里由于输入的地址是文字，需要将其返回json格式，也就是添加@RequsetBody注解
    private R saveDeliveryAddress(@RequestBody DeliveryAddress deliveryAddress){
        try {
            int i = deliveryAddressService.saveDeliveryAddress(deliveryAddress);
//
//            return R.ok().data(i);
            //            以下代码适合没有返回值的，例如添加，删除
            if (i>0){
                return R.ok().msg("添加成功");
            }else {
                return R.error().msg("添加失败");
            }

        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    @PostMapping("/updateDeliveryAddress")
    private R updateDeliveryAddress(@RequestBody DeliveryAddress deliveryAddress){
        try {
            int i = deliveryAddressService.updateDeliveryAddress(deliveryAddress);
            return R.ok().data(i);

        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @DeleteMapping("/removeDeliveryAddress")
    private R removeDeliveryAddress(@RequestBody DeliveryAddress deliveryAddress){
        try {
            int i = deliveryAddressService.removeDeliveryAddress(deliveryAddress);
//            return R.ok().data(i);
            //            以下代码适合没有返回值的，例如添加，删除
            if (i>0){
                return R.ok().msg("删除成功");
            }else {
                return R.error().msg("删除失败");
            }

        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @GetMapping("/listDeliveryAddressByUserId")
    private R listDeliveryAddressByUserId(String userId){
        try {
//            List<Business> businessList=deliveryAddressService.listDeliveryAddressByUserId(userId);
            return R.ok().data(deliveryAddressService.listDeliveryAddressByUserId(userId));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
